module RightHelper
  ##
  # This method verifies whether the logged in user is a student or not.
  # @param options is a hash which may contain :controller, :action, 
  #        and :message
	def verify_student(options = {})
		verify_right(1, options)
	end
  ##
  # This method verifies whether the logged in user is a teacher or not.
  # @param options is a hash which may contain :controller, :action, 
  #        and :message
	def verify_teacher(options = {})
		verify_right(2, options)
	end
  ##
  # This method verifies whether the logged in user is a admin or not.
  # @param options is a hash which may contain :controller, :action, 
  #        and :message
	def verify_admin(options = {})
		verify_right(4, options)
	end
	
	private
  ##
  # This method verifies whether the logged in user is has the corresponding 
  # right or not.
  # @param right is the right (= an integer) for which will be checked.
  # @param options is a hash which may contain :controller, :action, 
  #        and :message
	def verify_right(right, options = {})
		default_options = { :controller => :user, :action => :login , 
												:message => "Sorry, you don't have the " \
																		 "appropriate rights." }
		options = default_options.merge options
		if(session['user'].nil? || ((session['user'].rights & right) == 0 && 
                                (session['user'].group.rights & right) == 0))
			flash[:error] = options[:message]
			redirect_to :controller => options[:controller], 
									:action => options[:action]
		end
	end
end

